﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using RuleCode.Table;

namespace chatblog
{
    public partial class bbsview : System.Web.UI.Page
    {
        cbarea area;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                bind1();
                bind2();
                //bind3();
            }
            else
            {
                binduser();
            }
        }
        private void binduser()
        {
            if (AliApi.appUserId == "")
            {
                //Label_lastvisit.Text = "你还未登录,亲登陆.";
                Label_keyongjifen.Visible = false;
                Label_zhuanjiajifen.Visible = false;
                Label1_fatieshu.Visible = false;
                Label1_huitieshu.Visible = false;
            }
            else
            {
                cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
                //Label_lastvisit.Text = "上次登录时间:" + u.jclastdate.Date.ToString();
                Label_keyongjifen.Text = "可用分:" + u.cbpoint.ToString();
                Label_zhuanjiajifen.Text = "专家分:" + u.cbpointzhuanjia.ToString();
                Label1_fatieshu.Text = "发帖数:" + u.fatieshu.ToString();
                Label1_huitieshu.Text = "回帖数:" + u.huitieshu.ToString();
                Label_keyongjifen.Visible = true;
                Label_zhuanjiajifen.Visible = true;
                Label1_fatieshu.Visible = true;
                Label1_huitieshu.Visible = true;
            }
        }
        /// <summary>
        /// 绑定查看的帖子
        /// </summary>
        private void bind1()
        {
            HyperLink_existfile.Visible = false;
            if (this.Request.Params["cblistid"] != null)
            {
                cblist l = cblist.Find(" cblistid = '" + this.Request.Params["cblistid"].ToString() + "' ");
                if (l.Count > 0)
                {
                    if (l.cbuserid.ToString()==AliApi.appUserId)
                    {
                        HyperLink_edit.NavigateUrl = ResolveUrl("~/bbs/bbsedit.aspx")+"?cblistid=" + l.cblistid.ToString();
                        HyperLink_edit.Visible = true;
                    }
                    else
                    {
                        HyperLink_edit.Visible = false;
                    }
                    RuleCode.Table.cblist.afterview(l);
                    Label_title.Text = l.cbtitle.ToString();
                    Label_createdate.Text = l.jccreatedate.ToString();
                    //Label_time.Text = l.jccreatetime.ToString();
                    Label_fenshu.Text = "分数:"+l.fenshu.ToString();
                    Label_nickname1.Text = chatblog.zfunction.getnick(l.cbuserid.ToString());
                    Literal_content.Text = l.cbcontent.ToString();
                    cbusercata uc = cbusercata.Find(" cbuserid = '" + AliApi.appUserId
                        + "' and cbusercataid = '" + l.cbusercataid.ToString() + "' ");
                    Label_cata.Text = uc.cbname.ToString();
                    if (l.cbpath.ToString() != "")
                    {
                        HyperLink_existfile.Visible = true;
                        HyperLink_existfile.NavigateUrl = l.cbpath.ToString();
                        HyperLink_existfile.Text = "附件:" + l.cbpath.ToString();
                    }

                    cbuser u = cbuser.Find("cbuserid = '" + l.cbuserid.ToString() + "'");
                    HyperLink_addfriend.NavigateUrl = ResolveUrl("~/bbs/addfriend.aspx?recid=") + u.RECID.ToString();
                    if (u.cbimgpath.ToString() != "" && u.cbpoint.Value>=100)
                    {
                        Image_user.ImageUrl = u.cbimgpath.ToString();
                    }
                    Label_level.Text = "等级:百姓";
                    Label_zjf.Text = "专家分:"+u.cbpointzhuanjia.ToString();
                    Label_shijian.Text = "创建时间:" + u.jccreatedate.ToShortString();
                    Label_fatie.Text = "发帖数:" + u.fatieshu.ToString();
                    Label_huitie.Text = "回帖数:" + u.huitieshu.ToString();
                    Literal_qianming.Text = cbuser.getqianmingstring(u.cbuserid.ToString());

                    //HyperLink_default.Text = u.blogtitle.ToString();
                    if (AliApi.appDomainID != "")
                    {
                        cbuser u2 = cbuser.Find(" recid = " + AliApi.appDomainID);
                        TextBox_nick.Text = u2.cbusernickname.ToString();
                        TextBox_email.Text = u2.cbuserid.ToString();
                    }

                }
                else
                {
                    Label_title.Text = "你查看的帖子不存在";
                }

            }
            else
            {
                Response.Redirect(ResolveUrl("~/bbs.html"));
            }
            binduser();
            DataTable a1;
            string sql = "";
            //今日发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and jccreatedate <= '" + DateTime.Today.AddDays(1).ToShortDateString()
                + "' and jccreatedate >= '" + DateTime.Today.ToShortDateString() + "' "
               + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_today.Text = a1.Rows[0].ItemArray[0].ToString();
            //昨日发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and jccreatedate <= '" + DateTime.Today.ToShortDateString()
                + "' and jccreatedate >= '" + DateTime.Today.AddDays(-1).ToShortDateString() + "' "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_yestoday.Text = a1.Rows[0].ItemArray[0].ToString();
            //总发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_sum.Text = a1.Rows[0].ItemArray[0].ToString();
        }
        /// <summary>
        /// 回帖
        /// </summary>
        private void bind2()
        {
            Label_point.Visible = false;
            this.Panel_point.Visible = false;
            cblist l1 = cblist.Find(" cblistid = '" + this.Request.Params["cblistid"].ToString() + "'", "RECID");
            HyperLink_orgi.Visible = false;
            if (l1.cblisttype == "回帖")
            {
                string sql = " select * from vcblist where cblistid = '" + l1.pid.ToString() + "'";
                DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                //cblist l = cblist.Find(" cblistid = '" + l1.pid.ToString() + "'", "RECID");
                WebPager_return.DataSource = dt;
                WebPager_return.DataBind();
                if (dt.Rows.Count >0)
                {
                    HyperLink_orgi.NavigateUrl = ResolveUrl("~/bbsview/") + dt.Rows[0]["cblistid"].ToString() + ".html";
                    HyperLink_orgi.Visible = true;
                }
            }
            else
            {
                
                cblist l = cblist.Find(" pid = '" + this.Request.Params["cblistid"].ToString() + "'", "RECID");
                string sql = " select * from vcblist where  pid = '" + this.Request.Params["cblistid"].ToString() + "' order by RECID";
                DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                //dt.Columns.Add("lou", typeof(Int32));
                //int i= 0 ;
                //foreach (DataRow item in dt.Rows)
                //{
                //    item["lou"] = i;
                //}
                WebPager_return.DataSource = dt;
                WebPager_return.DataBind();
                bool canm = canmanage(l);
                foreach (DataListItem item in DataList_return.Items)
                {
                    LinkButton dl = item.FindControl("LinkButton_del") as LinkButton;
                    if (canm)
                    {
                        dl.Visible = true;
                    }
                    else
                    {
                        dl.Visible = false;
                    }
                }
                
                if (l1.cblisttype == "发帖" && l1.cbuserid.ToString() == AliApi.appUserId
                        && l1.fenshuNoYes == RuleCode.Enum.NoYes.Yes && l1.fenshu.Value > 0)
                {
                    Label_point.Text = "积分已分配完毕:" + l1.fenshu.ToString()+" "+l1.pointdesc.ToString();
                    Label_point.Visible = true;
                }
                if (l1.cblisttype == "发帖" && l1.cbuserid.ToString() == AliApi.appUserId
                        && l1.fenshuNoYes == RuleCode.Enum.NoYes.No && l1.fenshu.Value >0)
                {
                    Label_point.Text = "可分配积分:" + l1.fenshu.ToString();
                    Label_point.Visible = true;

                    DropDownList1.Items.Clear();
                    DropDownList1.Items.Add("");
                    DropDownList2.Items.Add("");
                    DropDownList3.Items.Add("");
                    DropDownList4.Items.Add("");
                    DropDownList5.Items.Add("");
                    foreach (DataRow item in l.MyTable.Rows)
                    {
                        string nick = zfunction.getnick(item["cbuserid"].ToString());
                        if (DropDownList1.Items.FindByText(nick) == null && item["cbuserid"].ToString()!=AliApi.appUserId)
                        {
                            ListItem li = new ListItem(zfunction.getnick(item["cbuserid"].ToString()), item["cbuserid"].ToString());
                            DropDownList1.Items.Add(li);
                            DropDownList2.Items.Add(li);
                            DropDownList3.Items.Add(li);
                            DropDownList4.Items.Add(li);
                            DropDownList5.Items.Add(li);
                        }
                    }
                    
                    if (DropDownList1.Items.Count > 0)
                    {
                        //DropDownList2.SelectedIndex = 0;
                        //DropDownList3.SelectedIndex = 0;
                        //DropDownList4.SelectedIndex = 0;
                        //DropDownList5.SelectedIndex = 0;
                        DropDownList1.SelectedIndex = 1;
                        TextBox1.Text = l1.fenshu.ToString();
                    }
                    if (DropDownList1.Items.Count > 1)
                    {
                        DropDownList2.SelectedIndex = 2;
                    }
                    if (DropDownList1.Items.Count > 2)
                    {
                        DropDownList3.SelectedIndex = 3;
                    }
                    if (DropDownList1.Items.Count > 3)
                    {
                        DropDownList4.SelectedIndex = 4;
                    }
                    if (DropDownList1.Items.Count > 4)
                    {
                        DropDownList5.SelectedIndex = 5;
                    }
                    this.Panel_point.Visible = true;
                }
                else
                {
                    this.Panel_point.Visible = false;
                }
                

            }

        }


        private bool canmanage(cblist l)
        {
            if (AliApi.appUserId == "")
            {
                return false;
            }
            cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            if (u.isadmin.Value == RuleCode.Enum.NoYes.Yes)
            {
                return true;
            }
            cbcata c = cbcata.Find(" cbcataid = '" + l.cbcataid.ToString() + "' ");
            cbbanzhu bz = cbbanzhu.Find(" cbuserid = '" + AliApi.appUserId + "'"
                + " and (cbcataid = '" + c.cbcataid.ToString()
                + "' or cbcataid ='" + c.pid1.ToString()
                + "' or cbcataid ='" + c.pid2.ToString()
                + "' or cbcataid ='" + c.pid3.ToString()
                + "' or cbcataid ='" + c.pid4.ToString()
                + "' )"
                );
            if (bz.Count > 0)
            {
                return true;
            }

            return false;
        }


        protected void Button_ok_Click(object sender, EventArgs e)
        {
            if (TextBox_nick.Text.Trim() == "" && AliApi.appNickName != "")
            {
                TextBox_nick.Text = AliApi.appNickName;
            }
            if (TextBox_email.Text.Trim() == "" && AliApi.appUserId != "")
            {
                TextBox_email.Text = AliApi.appUserId;
            }
            if (TextBox_nick.Text.Trim() == "")
            {
                Response.Write("<script>alert('请填写昵称!')</script>");
                return;
            }
            if (TextBox_email.Text.Trim() == "")
            {
                Response.Write("<script>alert('请填写邮件!')</script>");
                return;
            }
            if (Fck_Content.Value.Length < 15)
            {
                Response.Write("<script>alert('回复内容的长度要求大于10!')</script>");
                return;
            }
            cbuser u = cbuser.Find(" cbuserid = '" + TextBox_email.Text.Trim() + "'");
            if (u.Count > 0 && u.cbuserid.ToString() != AliApi.appUserId)
            {
                Response.Write("<script>alert('你填写的邮箱地址是本站用户,请先登录(右上角).')</script>");
                return;
            }
            cblist l1 = cblist.Find(" cblistid = '" + this.Request.Params["cblistid"].ToString()+ "'");
            cblist l = cblist.Find(" 1=2 ");
            l.New();
            l.cbtitle.Value = "re:" + l1.cbtitle.ToString();
            l.pid.Value = this.Request.Params["cblistid"].ToString();
            l.cbcataid.Value = l1.cbcataid.ToString();
            l.cbusercataid.Value = l1.cbusercataid.ToString();
            l.cbtitle.Value = "";
            l.cbcontent.Value = Fck_Content.Value;
            l.cblistid.Value = l.GetRecID().ToString();
            l.paixu.StringValue = l.cblistid.ToString();
            l.jccreatedate.Value = DateTime.Now;
            l.jccreatetime.StringValue = DateTime.Now.ToLongTimeString();
            l.cbuserid.Value = AliApi.appUserId;
            l.cbstatus.Value = "有效";
            l.cblisttype.Value = "回帖";
            l.cbuserid.Value = TextBox_email.Text;
            l.cbext.Value = TextBox_nick.Text;
            l.ip.Value = zfunction.IPAddress;
            l.Insert();
            RuleCode.Table.cblist.afterinsert(l);
            Response.Redirect(ResolveUrl("~/bbsview/") + this.Request.Params["cblistid"].ToString() + ".html");
        }
        static public string getemaillink(string email, string ext)
        {
            cbuser u = cbuser.Find(" cbuserid = '" + email + "'");
            if (u.Count > 0)
            {
                System.Web.UI.Control c = new System.Web.UI.Control();
                return "<a href='" + c.ResolveUrl("~/blog/") +  u.RECID.ToString() + ".html' style=\"font-size:medium; font-weight:bold;\">" + ext + "</a>";
            }
            else
            {
                return ext;
            }
        }

        static public string getnewlink()
        {
            cblist l1 = cblist.Find(" cblistid = '" + System.Web.HttpContext.Current.Request.Params["cblistid"].ToString() + "'", "RECID");
            System.Web.UI.Control c = new System.Web.UI.Control();
            return "<a href="+c.ResolveUrl("~/bbs/bbsedit.aspx?cbcataid=") + l1.cbcataid.ToString()
                + " onfocus=\"this.blur();\"><img src=\"../images/newtopic.gif\" style=\"border:0;\" /></a> ";
                ;
        }

        protected void DataList_return_ItemDataBound(object sender, DataListItemEventArgs e)
        {

        }

        protected void DataList_return_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string id = Convert.ToString(DataList_return.DataKeys[e.Item.ItemIndex]);
            if (e.CommandName == "del")
            {
                cblist l = cblist.Find(" recid = " + id);
                l.Delete();
                bind2();
                // Response.Write("<script>alert('删除成功!')</script>");

            }
        }

        private void dealrequest()
        {
            area = cbarea.getcurarea();
            HyperLink_link.Text = area.cbtitle.ToString();
            string link = "";
            cblist lt = cblist.Find(" cblistid = '" + this.Request.Params["cblistid"].ToString() + "'");

            Literal_chatroom.Text = chatblog.zfunction.getchatroomlink("1",lt.RECID.ToString());

            this.Title = area.cbtitle.ToString();
            this.Title = lt.cbtitle.ToString() + "-" + this.Title;
            cbcata ct = cbcata.Find(" cbcataid = '" + lt.cbcataid.ToString() + "' ");
            while (ct.Count > 0 && (ct.pid1.ToString() != "0"
                && ct.cbcataid.ToString() != area.cbcataid.ToString()) ||
                (ct.cbstatus.ToString() == "全部"))
            {
                link = "-" + "<a href='" + ResolveUrl("~/bbscata/") + ct.cbcataid.ToString() + ".html'>"
                                            + ct.cbname.ToString() + "</a>" + link;
                ct = cbcata.Find(" cbcataid = '" + ct.pid1.ToString() + "' ");
            }
            cbbanzhu bz = cbbanzhu.Find(" cbcataid = '" + lt.cbcataid.ToString() + "' ");
            if (bz.Count < 1)
            {
                link += " 版主:空缺 ";
            }
            else
            {
                link += " 版主: ";
                foreach (DataRow item in bz.MyTable.Rows)
                {
                    cbuser u = cbuser.Find( " cbuserid = '"+ item["cbuserid"].ToString()+"'");
                    link += "<a href='" + ResolveUrl("~/blog/") + u.RECID.ToString() + ".html' target=\"_blank\">"
                                +u.cbusernickname.ToString()+"</a> &nbsp;";
                }
            }

            this.Literal_link.Text = link;


            Literal_logolink.Text = area.getlogolink();
            Literal_newbtn.Text = getnewlink();
            Literal_newbtn2.Text = Literal_newbtn.Text;
        }

        protected void WebPager_return_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            bind2();
            this.WebPager_return.CurrentPageIndex = e.NewPageIndex;
        }

        protected void Button_point_Click(object sender, EventArgs e)
        {
            cblist l1 = cblist.Find(" cblistid = '" + this.Request.Params["cblistid"].ToString() + "'");
            if (l1.fenshuNoYes.Value == RuleCode.Enum.NoYes.Yes)
            {
                return;
            }
            int jifen = 0;
            if (DropDownList1.Text != "" && zfunction.str2int(TextBox1.Text)>0)
            {
                jifen += zfunction.str2int(TextBox1.Text);
            }
            if (DropDownList2.Text != "" && zfunction.str2int(TextBox2.Text)>0)
            {
                jifen += zfunction.str2int(TextBox2.Text);
            }
            if (DropDownList3.Text != "" && zfunction.str2int(TextBox3.Text)>0)
            {
                jifen += zfunction.str2int(TextBox3.Text);
            }
            if (DropDownList4.Text != "" && zfunction.str2int(TextBox4.Text)>0)
            {
                jifen += zfunction.str2int(TextBox4.Text);
            }
            if (DropDownList5.Text != "" && zfunction.str2int(TextBox5.Text)>0)
            {
                jifen += zfunction.str2int(TextBox5.Text);
            }
            if (l1.fenshu.Value != jifen)
            {
                MessageBox.Show(this.Page, "分配积分的总数应该为:"+l1.fenshu.ToString());
                return;
            }
            
            string pointdesc = "";
            if (DropDownList1.Text != "" && zfunction.str2int(TextBox1.Text)>0)
            {
                cbuser.setzjf(DropDownList1.SelectedItem.Value, zfunction.str2int(TextBox1.Text));
                pointdesc += DropDownList1.SelectedItem.Value+ ":"+zfunction.str2int(TextBox1.Text).ToString()+ " ";
            }
            if (DropDownList2.Text != "" && zfunction.str2int(TextBox2.Text) > 0)
            {
                cbuser.setzjf(DropDownList2.SelectedItem.Value, zfunction.str2int(TextBox2.Text));
                pointdesc += DropDownList2.SelectedItem.Value + ":" + zfunction.str2int(TextBox2.Text).ToString() + " ";
            }
            if (DropDownList3.Text != "" && zfunction.str2int(TextBox3.Text) > 0)
            {
                cbuser.setzjf(DropDownList3.SelectedItem.Value, zfunction.str2int(TextBox3.Text));
                pointdesc += DropDownList3.SelectedItem.Value + ":" + zfunction.str2int(TextBox3.Text).ToString() + " ";
            }
            if (DropDownList4.Text != "" && zfunction.str2int(TextBox4.Text) > 0)
            {
                cbuser.setzjf(DropDownList4.SelectedItem.Value, zfunction.str2int(TextBox4.Text));
                pointdesc += DropDownList4.SelectedItem.Value + ":" + zfunction.str2int(TextBox4.Text).ToString() + " ";
            }
            if (DropDownList5.Text != "" && zfunction.str2int(TextBox5.Text) > 0)
            {
                cbuser.setzjf(DropDownList5.SelectedItem.Value, zfunction.str2int(TextBox5.Text));
                pointdesc += DropDownList5.SelectedItem.Value + ":" + zfunction.str2int(TextBox5.Text).ToString() + " ";
            }
            l1.fenshuNoYes.Value = RuleCode.Enum.NoYes.Yes;
            l1.pointdesc.Value = pointdesc;
            l1.Modify();//分配给谁了
            MessageBox.Show(this.Page, "操作成功");
            bind2();
        }



    }
}
